﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DomainInfoLib
{
    public class DynadotAPI:IWebAPI
    {
        private static bool? ProcessAvaliableStr(string avaliableStr)
        {

            if (string.IsNullOrEmpty(avaliableStr))
            {
                return null;
            }

            int pos = avaliableStr.LastIndexOf(',');

            if (pos == -1)
            {
                throw new Exception("CnolnicAPI:" + avaliableStr);
            }

            string key = avaliableStr.Substring(pos - 3, 3);

            if (key == "yes")
            {
                return true;
            }

            return false;
        }


        private static string ProcessXmlStr(string XmlStr)
        {
            try
            {
                if (string.IsNullOrEmpty(XmlStr)) return "";



                return XmlStr;
            }
            catch
            {

                return "";
            }



        }

        public string GetUrl(string DomainName)
        {
            string url = string.Format(
                                        "https://api.dynadot.com/api.html?version=1.0&key=c6Q8X6h6h609D658p8Qw7g6G7W8O8k9J8S9GLxRc7k&command=search&domain0={0}"
                                        , DomainName
                                       );

            return url;
        }

        public string GetUrlBatch(string ParamsStr)
        {
            string url = string.Format(
                                        "https://api.dynadot.com/api.html?version=1.0&key=c6Q8X6h6h609D658p8Qw7g6G7W8O8k9J8S9GLxRc7k&command=search{0}"
                                        , ParamsStr
                                       );

            return url;
        }

        public RegisterInfo GetResponseByWebAPI(string DomainName)
        {

            string url = GetUrl(DomainName);

            string xmlstr = BaseInfo.CallWebPage(url, null, null);

            if (xmlstr == "domainquerymsg='请至少选择一种类型'")
            {
                throw new Exception(url);
            }

            string avaliableStr = ProcessXmlStr(xmlstr);

            bool? Avaliable = ProcessAvaliableStr(avaliableStr);

            if (!Avaliable.HasValue)
            {
                return null;
            }

            return RegisterInfo.BuildRegisterInfo(DomainName, xmlstr, Avaliable, this.GetType().Name);
        }

        public RegisterInfo GetResponseByWebAPI1(string paramsStr)
        {
            string url = GetUrlBatch(paramsStr);

            string xmlstr = BaseInfo.CallWebPage(url, null, null);

            if (xmlstr == "domainquerymsg='请至少选择一种类型'")
            {
                throw new Exception(url);
            }

            string avaliableStr = ProcessXmlStr(xmlstr);

            bool? Avaliable = ProcessAvaliableStr(avaliableStr);

            if (!Avaliable.HasValue)
            {
                return null;
            }

            return RegisterInfo.BuildRegisterInfo(paramsStr, xmlstr, Avaliable, this.GetType().Name);
        }
    }
}
